﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ESupervision.Models;

namespace ESupervision.Controllers
{
    [Authorize(Roles = "Teacher")]
    public class MeetingEventParticipantController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        // GET: /MeetingEventParticipant/
        public async Task<ActionResult> Index(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            MeetingEvent meetingevent = await db.MeetingEvents.FindAsync(id);
            if (meetingevent == null)
            {
                return HttpNotFound();
            }
            ViewBag.MEventID = id;
            var student = from stu in db.Students
                          where stu.SupervisorUserId == meetingevent.OrganizerUserID || stu.SecondMarkerUserId == meetingevent.OrganizerUserID
                          select stu;
            var requested = from p in db.MeetingEventParticipants
                            where p.MeetingEventID == id
                            select p.RequestedUser;

            var meetingeventparticipants = db.MeetingEventParticipants.Where(p => p.MeetingEventID == id).Include(m => m.MeetingEvent).Include(m => m.RequestedUser);
            ViewBag.ListOfStudents = new SelectList(student.Except(requested), "Id", "FullName");
            return View(await meetingeventparticipants.ToListAsync());
        }



        // POST: /MeetingEventParticipant/Request
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> MeetingRequest(int EventID, string[] MeetingRequest)
        {
            foreach (var userId in MeetingRequest)
            {
                ApplicationUser requested = db.Users.Find(userId);
                if (requested != null)
                {
                    MeetingEventParticipant meetingeventparticipant = new MeetingEventParticipant
                    {
                        MeetingEventID = EventID,
                        RequestedUserID = userId
                    };
                    db.MeetingEventParticipants.Add(meetingeventparticipant);
                }
            }
            await db.SaveChangesAsync();
            return RedirectToAction("Index/" + EventID);
        }

        public async Task<ActionResult> Delete(int id)
        {
            MeetingEventParticipant meetingeventparticipant = await db.MeetingEventParticipants.FindAsync(id);
            db.MeetingEventParticipants.Remove(meetingeventparticipant);
            await db.SaveChangesAsync();
            return RedirectToAction("Index/" + meetingeventparticipant.MeetingEventID);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
